package com.template.web.utils;

import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class JsonResult<T> implements Serializable {

    @Getter
    @Setter
    private Boolean success;

    @Getter
    @Setter
    private Integer code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T data;

    @Getter
    @Setter
    private Long timestamp;

    public Boolean isSuccess() {
        return success;
    }

    public static <T> JsonResult<T> success() {
        return result(true, 200, null, null);
    }

    public static <T> JsonResult<T> success(T data) {
        return result(true, 200, null, data);
    }

    public static <T> JsonResult<T> success(String msg, T data) {
        return result(true, 200, msg, data);
    }

    public static <T> JsonResult<T> failed() {
        return result(false, 500, null, null);
    }

    public static <T> JsonResult<T> failed(String msg) {
        return result(false, 500, msg, null);
    }

    public static <T> JsonResult<T> failed(String msg, T data) {
        return result(false, 500, msg, data);
    }

    public static <T> JsonResult<T> failed(Integer code, String msg) {
        return result(false, code, msg, null);
    }

    private static <T> JsonResult<T> result(Boolean success, Integer code, String msg, T data) {
        JsonResult<T> result = new JsonResult<>();
        result.setSuccess(success);
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        result.setTimestamp(System.currentTimeMillis());
        return result;
    }
}
